<template>
  <div class="componentsbox" >
     <!-- 面包屑 -->
      <el-table
      :data="list"
      style="width: 900px;margin: 20px auto;" height="510px">
      <el-table-column label="序号" align="center" type="index" width="60" >
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="planId"
        label="保险名称"
        width="180">
        <template slot-scope="scope">
          <el-link @click="godetail(scope.row)">{{getbxname(scope.row)}}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="policyMoney"
        label="保险金额"
        width="100">
      </el-table-column>
      <el-table-column
        prop="sTime"
        label="保险时间" min-width="120px">
      </el-table-column>
       <el-table-column
        prop="orderStatus"
        label="投保状态">
        <template slot-scope="scope">
          <span >{{getorderStatus(scope.row.orderStatus)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="140px">
          <template slot-scope="scope">
            <el-button type="danger" v-if="scope.row.orderStatus==0" size="mini" @click="handleDelete(scope.row)">删除</el-button>
             <el-button type="primary" v-if="scope.row.orderStatus==0" size="mini" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="primary" v-if="scope.row.orderStatus==1" size="mini" @click="querenbd(scope.row)">确认保单</el-button>
            <el-button type="primary" v-if="scope.row.orderStatus==4" size="mini" @click="xiazai(scope.row)">下载投保单</el-button>
            <el-button type="primary" v-if="scope.row.orderStatus==4" size="mini" @click="shangchuan(scope.row)">上传</el-button>
            <el-button type="primary" v-if="scope.row.orderStatus==13&&scope.row.policyUrl||scope.row.orderStatus==11&&scope.row.policyUrl" size="mini" @click="yulanbaodan(scope.row)">电子保单</el-button>
          </template>
      </el-table-column>
    </el-table>
    <div style="width: 900px;margin: 20px auto;"  >
      <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="init"
    />
    </div>
    

    <el-dialog
      title="上传"
      :visible.sync="dialogVisible"
      width="620px"
    >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">
        <el-form-item label="投保单" prop="protectUrl">
          <el-upload
           v-model="ruleForm.protectUrl"
            class="upload-demo"
            :action="qiniuUrl"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
            :data="upData"
            accept=".pdf"
            :limit="1"
            :file-list="fileList"
            :multiple="false"
            >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">*请上传下载的PDF文件加盖印章！</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="缴费凭证" prop="payVoucherUrl">
          <el-upload
          v-model="ruleForm.payVoucherUrl"
            class="upload-demo"
            :action="qiniuUrl"
            :on-preview="handlePreview2"
            :on-remove="handleRemove2"
            :on-success="handleSuccess2"
            :before-upload="beforeUpload2"
            :data="upData"
            :limit="1"
            :file-list="fileList2"
            :multiple="false"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2m</div>
          </el-upload>
        </el-form-item>
        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mybxlist,delbx,shangchuanimg,qiniutoken } from '@/api/bx';
export default {
  name: "goodsBox",
  data() {
    return {
      loading:false,
      list:[],
      queryParams:{
        pageNum:1,
        pageSize:10,
      },
      total:0,
      dialogVisible:false,
      qiniuUrl:'',
      realmName:'',
      upData:{
        token: '',
        key: '',
        bucket: "tiyulife",
      },
      fileList: [], 
      fileList2: [],
      ruleForm: {},
      rules: {
        protectUrl: [
          { required: true, message: '投保单不能为空', trigger: 'change' }
        ],
        payVoucherUrl: [
          { required: true, message: '缴费凭证不能为空', trigger: 'change' }
        ],
      },
    };
  },
  
  mounted() {
    this.init()
    this.getToken()
    this.qiniuUrl = this.$qiniuUrl
    this.realmName = this.$realmName
     
    
  },
  
  methods: {
    handleEdit(row){
      var type=0
      if(row.planId){
        type=row.planId
      }
      this.$router.push({
        path: '/insureAdd',
        query: {
          id: row.id,
          type,
        }
      })
    },
    submitForm(){
      console.log(this.ruleForm)

      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const res =await shangchuanimg(this.ruleForm)
          this.dialogVisible=false
          this.init()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //获取七牛token
    getToken() {
      qiniutoken().then(res => {
        // console.log(res)
        this.upData.token = res.msg
      })
    },
    shangchuan(row){
      this.ruleForm.mainNum=row.mainNum
      this.dialogVisible=true
    },
    xiazai(item){
      if(!item.protectUrl)return
      const a = document.createElement('a');
      a.href = item.protectUrl;
      const fileName = item.protectUrl.split('/').pop();
      const fileExtension = fileName.split('.').pop();
      // 设置下载文件名
      a.download = '投保单'+'.'+fileExtension;
      a.target='_blank'

      // 触发下载
      document.body.appendChild(a);
      a.click();
      // 清理资源
      document.body.removeChild(a);
    },
    yulanbaodan(item){
      window.location.href = item.policyUrl
    },
    querenbd(item){
      window.location.href = item.insureUrl
    },
    handleDelete(row){
      this.$confirm('确定删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delbx({id:row.id}).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });          
      });
    },
    getorderStatus(orderStatus){
      switch (orderStatus) {
        case 0:
          return '草稿'
        case 1:
          return '待确认'
        case 4:
          return '待签章'
        case 6:
          return '待出单'
        case 11:
          return '已出单（未生效）'
        case 13:
          return '保障中（已生效）'
        case 14:
          return '订单失效'
        default:
          return '--'
      }
    },
    async init(){
      this.loading=true
      
      const res =await mybxlist(this.queryParams)
      console.log(res)
      this.list=res.rows
      this.total=res.total
      this.loading = false
    },
    godetail(item){
      var type=0
      if(item.productId==1&&item.planId==1){
        type=1
      }else if(item.productId==1&&item.planId==2){
        type=2
      }else if(item.productId==1&&item.planId==3){
        type=3
      }
      this.$router.push({path:"/orderDetail", query:{type,id:item.id} })
    },
    getbxname(item){
      switch (item.planId) {
        case 1:
          return '电梯责任险套餐一'
        case 2:
          return '电梯责任险套餐二'
        case 3: 
          return '电梯责任险套餐三'
        default:
          return '--'
      }
    },
    beforeUpload(file) {
      console.log(file)
      var now=new Date().getTime()
      this.upData.key= now +'-'+ file.name
    },
    handleSuccess(res, file){
      console.log(res, file);
      this.$set(this.ruleForm, 'protectUrl', this.realmName + res.key);
    },
     handleRemove(file, fileList) {
        console.log(file, fileList);
        this.ruleForm.protectUrl=''
      },
      handlePreview(file) {
        console.log(file);
      },
      beforeUpload2(file) {
        console.log(file)
        var now=new Date().getTime()
        this.upData.key= now +'-'+ file.name
      },
      handleSuccess2(res, file){
        this.$set(this.ruleForm, 'payVoucherUrl', this.realmName + res.key);

      console.log(res, file);
    },
     handleRemove2(file, fileList) {
        console.log(file, fileList);
        this.ruleForm.payVoucherUrl=''
      },
      handlePreview2(file) {
        console.log(file);
      }
  },
 
  
};
</script>
<style scoped lang="scss">

.componentsbox{
  width: 100%;
  height: 100%;
  .goodsbox{
    width: 100%;
    // max-height: 500px;
    overflow: auto;
    display: flex;
    flex-wrap: wrap;
    gap:1%;
    .goodsitem{
      flex: 0 0 auto;
      width: 24%;
      // height: 354px;
      background: #FFFFFF;
      border: 1px solid #FFFFFF;
      // box-shadow: 0px 10px 21px 0px rgba(16,41,60,0.05), 0px 39px 39px 0px rgba(16,41,60,0.04), 0px 88px 53px 0px rgba(16,41,60,0.03), 0px 156px 62px 0px rgba(16,41,60,0.01), 0px 243px 68px 0px rgba(16,41,60,0);
      border-radius: 8px 8px 8px 8px;
      margin-bottom: 10px;
      padding-bottom: 4px;
      box-sizing: border-box;
      cursor: pointer;
      overflow: hidden;
      .goodsitem_img{
        width: 100%;
        height: 284px;
        img{
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
      .goodsitem_title{
        font-weight: 400;
        font-size: 16px;
        color: #000000;
        text-align: left;
        margin: 16px 0 24px 0;
      }
      .gooditem_money{
        padding: 0 4px 0 4px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        .qianqian{
          display: flex;
          align-items: flex-end;
          color: #DB4444;
          text-align: left;
          .danwei{
            font-size: 16px;
          }
          .qianvalue{
            font-weight: bold;
            font-size: 32px;
          }
        }
        .xiaoliang{
            font-weight: 400;
            font-size: 14px;
            color: #000000;
            text-align: right;
        }
      
      }
    }
    // .goodsitem:hover{
    //   border: 1px solid #0672F9;
    // }
  }
  .loading,.no-more{
    text-align: center;
    margin-top: 20px;
    font-size: 32px;
    color: #DB4444;
  }
}
</style>
